--[[ Copyright 2012, Chris Ho                               ]]--
--[[ MIT License (Please read the LICENSE file for details) ]]--

Use("common/Const");
Use("common/Log");
Use("common/Task");
Use("common/Pet");
Use("common/Skill");
Use("tasks/Charge");

-- I am a Tier 2 Swordsman.
local name = "F02";
local Initialize, status = loadfile(PERSONALITY_SWORDMAN);
Info(name .. ": trying fencer personality");
if (Initialize == nil) then
  Info(name .. ": trying default personality (" .. status .. ")");
  Initialize, status = loadfile(PERSONALITY_DEFAULT);
  if (Initialize == nil) then
    Alert(name .. ": " .. status);
    return ME;
  end
end

-- Initialize base personality.
--local instance = Initialize(...);
--local instance = Initialize(arg[1]);
local instance = Initialize();



--[[ Select an attack for this target ]]--
function instance:Attack(_target)
  if (self.target ~= _target) then
    -- Changing targets resets the combo.
    self.combo = 0;
  end

  -- Calculate overall danger level and the target's preferability.
  local danger = self:Assess(ACTORS) + self.owner:Assess(ACTORS);
  local score = self:RateArea(Position.Find(_target), 2);
  local sp = GetV(V_SP, self.id);

  -- Attack normally unless it's crowded.
  local skill = Skill.None(self.id);

  -- Adjust score and danger by tranquility.
  danger = danger / self:GetTranquility();
  score = score * score / self:GetTranquility();

  -- MINIMUM SP: 8 for Provoke (Level 5)
  if (sp >= 8) then
    if ((self.combo == 0) or (self.combo == 2)) then
      if (sp > (self.maxsp * (1.0 - danger))) then
        -- Force combo to position 1.
        self.combo = 1;

        -- This is our first attack. Use Provoke.
        self.target = _target;
        self.task = Charge.Create(self, Skill.Create(self.id, SKILL_PROVOKE, 5), self.target);
        Info(name .. ": Chose debuff skill " .. self.task.skill.code .. " level " .. self.task.skill.level .. " for " .. _target .. " (danger=" .. danger .. ", score=" .. score .. ")");
        return;
      end
    end
  end

  -- MINIMUM SP: 30 for Magnum Break
  if (sp >= 30) then
    -- Test area attack feasibility.
    if ((sp * score) > self.maxsp) then
      -- Use an area of effect attack (Magnum Break)
      self.target = _target;
      self.task = Charge.Create(self, Skill.Create(self.id, SKILL_MAGNUMBREAK, 3), self.target);
      Info(name .. ": Chose area skill " .. self.task.skill.code .. " level " .. self.task.skill.level .. " for " .. _target .. " (danger=" .. danger .. ", score=" .. score .. ")");
      return;
    end
  end

  -- Execute generic atatck.
  self.target = _target;
  self.task = Charge.Create(self, skill, self.target);
end

--[[ The pet should help the given target ]]--
-- We cast Provoke on the owner's target.
function instance:Help(_target)
  local threat = GetV(V_TARGET, _target);
  if (threat ~= 0) then
    -- Combo 2 means to force Provoke.
    Info(name .. ": assisting " .. _target);
    self.combo = 2;
    self:Attack(threat);
  else
    Persona.Help(self, _target);
  end
end

--[[ Override Default Rating Calculation ]]--
function instance:RateTarget(_id)
  local score = Fencer.RateTarget(self, _id);
  local there = Position.Find(_id);

  -- Tier 2 fencers also consider the target's regional preference (for
  -- Magnum Break).
  return score + self:RateArea(there, 2);
end

Info(name .. " Loaded");
return instance;
